package com.makeunion.gdlbsdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.makeunion.gdlbsdemo.R;

/**
 * <p>write the description
 *
 * @author renjialiang
 * @version [版本号]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MapUISettingActivity extends AppCompatActivity implements
        LocationSource, AMapLocationListener {

    private MapView mapView;

    private AMap aMap;

    private CheckBox mBiLiCheckBox;

    private CheckBox mZhiNanCheckBox;

    private CheckBox mScaleCheckBox;

    private UiSettings mUiSettings;

    private LocationSource.OnLocationChangedListener mListener;

    private AMapLocationClient mLocationClient;

    private AMapLocationClientOption mLocationOption;

    private CompoundButton.OnCheckedChangeListener checker4 = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mUiSettings.setScaleControlsEnabled(b);
        }
    };

    private CompoundButton.OnCheckedChangeListener checker5 = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mUiSettings.setCompassEnabled(b);
        }
    };

    private CompoundButton.OnCheckedChangeListener checker6 = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mUiSettings.setZoomControlsEnabled(b);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_ui_setting);

        mBiLiCheckBox = (CheckBox)findViewById(R.id.cb_bi_li);
        mZhiNanCheckBox = (CheckBox)findViewById(R.id.cb_zhi_nan);
        mScaleCheckBox = (CheckBox)findViewById(R.id.cb_scale);

        mBiLiCheckBox.setOnCheckedChangeListener(checker4);
        mZhiNanCheckBox.setOnCheckedChangeListener(checker5);
        mScaleCheckBox.setOnCheckedChangeListener(checker6);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();

        mUiSettings.setScrollGesturesEnabled(false);
        mUiSettings.setZoomGesturesEnabled(false);
        mUiSettings.setTiltGesturesEnabled(false);
        mUiSettings.setRotateGesturesEnabled(false);
        mUiSettings.setAllGesturesEnabled (false);
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
            mUiSettings = aMap.getUiSettings();
        }
    }

    public void showBiLi(View view) {
        float scale = aMap.getScalePerPixel();
        Toast.makeText(MapUISettingActivity.this, "每像素代表" + scale + "米", Toast.LENGTH_SHORT).show();
    }

    public void setScaleLocation(View view) {
        mUiSettings.setZoomPosition(mUiSettings.getZoomPosition() == AMapOptions.ZOOM_POSITION_RIGHT_CENTER
                ? AMapOptions.ZOOM_POSITION_RIGHT_BUTTOM : AMapOptions.ZOOM_POSITION_RIGHT_CENTER);
    }

    public void setLogoLocation(View view) {
        mUiSettings.setLogoPosition(AMapOptions.LOGO_MARGIN_LEFT);
        mUiSettings.setLogoPosition(AMapOptions.LOGO_MARGIN_BOTTOM);
        mUiSettings.setLogoPosition(AMapOptions.LOGO_MARGIN_RIGHT);
        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);
        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);
        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_CENTER);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (mListener != null && aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //这里高德地图进行了高度封装，直接就显示系统小蓝点
                mListener.onLocationChanged(aMapLocation);
            } else {
                Log.e("TAG", aMapLocation.getErrorCode() + ":" + aMapLocation.getErrorInfo());
                Toast.makeText(this, aMapLocation.getErrorCode() + ":" + aMapLocation.getErrorInfo(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        if (mLocationClient == null) {
            mLocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            //设置定位监听
            mLocationClient.setLocationListener(new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {

                }
            });
            mLocationOption.setInterval(1000);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            mLocationClient.startLocation();
        }
    }

    @Override
    public void deactivate() {
        mListener = null;
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
    }
}
